<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" size="default">
					<el-form-item label="参数名称" prop="configName">
						<el-input
							v-model="tableData.param.configName"
							placeholder="请输入参数名称"
							clearable
							@keyup.enter.native="dataList"
							style="width: 180px"
						/>
					</el-form-item>
					<el-form-item label="参数键名" prop="configKey">
						<el-input
							v-model="tableData.param.configKey"
							placeholder="请输入参数键名"
							clearable
							@keyup.enter.native="dataList"
							style="width: 180px"
						/>
					</el-form-item>
					<el-form-item label="系统内置" prop="configType" style="width: 200px">
						<el-select v-model="tableData.param.configType" placeholder="系统内置" clearable style="width: 240px">
							<el-option label="是" value="1" />
							<el-option label="否" value="0" />
						</el-select>
					</el-form-item>
					<el-form-item label="创建时间" prop="dateRanges">
						<el-date-picker
							v-model="tableData.param.dateRanges"
							style="width: 240px"
							value-format="YYYY-MM-DD HH:mm:ss"
							type="daterange"
							range-separator="-"
							start-placeholder="开始日期"
							end-placeholder="结束日期"
						></el-date-picker>
					</el-form-item>
					<el-form-item>
						<el-button size="default" type="primary" class="ml10" @click="dataList">
							<el-icon>
								<ele-Search />
							</el-icon>
							查询
						</el-button>
						<el-button size="default" @click="resetQuery(queryRef)">
							<el-icon>
								<ele-Refresh />
							</el-icon>
							重置
						</el-button>
						<el-button size="default" type="success" class="ml10" @click="onOpenAddDic" v-auth="'api/v1/system/config/add'">
							<el-icon>
								<ele-FolderAdd />
							</el-icon>
							新增参数
						</el-button>
						<el-button size="default" type="danger" class="ml10" @click="onRowDel(null)" v-auth="'api/v1/system/config/delete'">
							<el-icon>
								<ele-Delete />
							</el-icon>
							删除参数
						</el-button>
					</el-form-item>
				</el-form>
			</div>
			<el-table :data="tableData.data" style="width: 100%" @selection-change="handleSelectionChange" stripe size="default">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="参数主键" align="center" prop="configId" width="100" />
				<el-table-column label="参数名称" align="center" prop="configName" :show-overflow-tooltip="true" />
				<el-table-column label="参数键名" align="center" prop="configKey" :show-overflow-tooltip="true" />
				<el-table-column label="参数键值" align="center" prop="configValue" :show-overflow-tooltip="true" />
				<el-table-column label="系统内置" align="center" prop="configType">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.configType === 1">是</el-tag>
						<el-tag type="info" v-else>否</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
				<el-table-column label="更新时间" align="center" prop="updateTime" width="180" :formatter="formatter" />
				<el-table-column label="创建时间" align="center" prop="createTime" width="180" :formatter="formatter" />
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditDic(scope.row)" v-auth="'api/v1/system/config/edit'">修改 </el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)" v-auth="'api/v1/system/config/delete'">删除 </el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination
				v-show="tableData.total > 0"
				:total="tableData.total"
				v-model:page="tableData.param.pageNum"
				v-model:limit="tableData.param.pageSize"
				@pagination="dataList"
			/>
		</el-card>
		<EditConfig ref="editConfigRef" @dataList="dataList" />
	</div>
</template>

<script lang="ts">
import { defineComponent, getCurrentInstance, onMounted, reactive, ref, toRefs } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import EditConfig from '/@/views/system/config/component/editConfig.vue';
import { deleteConfig, getConfigList } from '/@/api/system/config';

// 定义接口来定义对象的类型
interface TableDataRow {
	configId: number;
	configName: string;
	configKey: string;
	configValue: string;
	configType: number;
	remark: string;
	updateTime: string;
	createTime: string;
}

interface TableDataState {
	ids: number[];
	tableData: {
		data: Array<TableDataRow>;
		total: number;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
			configName: string;
			configKey: string;
			configType: string;
			dateRanges: string[];
			dateRange: string;
		};
	};
}

export default defineComponent({
	name: 'systemConfigList',
	components: { EditConfig },
	setup() {
		const { proxy } = getCurrentInstance() as any;
		const editConfigRef = ref();
		const queryRef = ref();
		const state = reactive<TableDataState>({
			ids: [],
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					dateRange: '',
					dateRanges: [],
					pageNum: 1,
					pageSize: 10,
					configName: '',
					configKey: '',
					configType: '',
				},
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			dataList();
		};
		const dataList = () => {
			state.tableData.param.dateRange = JSON.stringify(state.tableData.param.dateRanges);
			getConfigList(state.tableData.param).then((res: any) => {
				state.tableData.data = res.data.list;
				state.tableData.total = res.data.total;
			});
		};
		// 打开新增弹窗
		const onOpenAddDic = () => {
			editConfigRef.value.openDialog();
		};
		// 打开修改弹窗
		const onOpenEditDic = (row: TableDataRow) => {
			editConfigRef.value.openDialog(row);
		};
		// 删除
		const onRowDel = (row: TableDataRow) => {
			let msg = '确定要删除所选数据吗';
			let ids: number[] = [];
			if (row) {
				msg = `此操作将永久删除：“${row.configName}”，是否继续`;
				ids = [row.configId];
			} else {
				ids = state.ids;
			}
			if (ids.length === 0) {
				ElMessage.error('请选择要删除的数据');
				return;
			}
			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					deleteConfig(ids).then(() => {
						ElMessage.success('删除成功');
						dataList();
					});
				})
				.catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			dataList();
		};
		// 多选框选中数据
		const handleSelectionChange = (selection: TableDataRow[]) => {
			state.ids = selection.map((item) => item.configId);
		};
		// 格式化时间
		const formatter = (row: any, column: any, cellValue: any) => {
			return proxy.moment.unix(cellValue).format('YYYY-MM-DD HH:mm:ss');
		};
		return {
			editConfigRef,
			queryRef,
			onOpenAddDic,
			onOpenEditDic,
			onRowDel,
			dataList,
			resetQuery,
			handleSelectionChange,
			formatter,
			...toRefs(state),
		};
	},
});
</script>
<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;

		.el-table {
			flex: 1;
		}
	}
}
</style>
